import {constantRoutes} from '@/router'
import {getRouters} from '@/api/menu'
import Layout from '@/layout/index'

const permission = {
  state: {
    routes: [],
    addRoutes: []
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    }
  },
  actions: {
    // 生成路由
    GenerateRoutes({commit}) {
      return new Promise(resolve => {
        // 向后端请求路由数据
        const res = {
          "msg": "操作成功",
          "code": 200,
          "data": [
            {
              "name": "System",
              "path": "/system",
              "hidden": false,
              "redirect": "noRedirect",
              "component": "Layout",
              "alwaysShow": true,
              "meta": {
                "title": "商圈连锁",
                "icon": "system",
                "noCache": false
              },
              "children": [
                {
                  "name": "Circle",
                  "path": "circle",
                  "hidden": false,
                  "component": "system/circle/index",
                  "meta": {
                    "title": "商圈管理",
                    "icon": "form",
                    "noCache": false
                  }
                },
                {
                  "name": "Chain",
                  "path": "chain",
                  "hidden": false,
                  "component": "system/chain/index",
                  "meta": {
                    "title": "连锁管理",
                    "icon": "form",
                    "noCache": false
                  }
                }
              ]
            },
            {
              "name": "System",
              "path": "/system/partner",
              "hidden": false,
              "redirect": "noRedirect",
              "component": "Layout",
              "alwaysShow": true,
              "meta": {
                "title": "机构管理",
                "icon": "system",
                "noCache": false
              },
              "children": [
                {
                  "name": "Partner",
                  "path": "partner",
                  "hidden": false,
                  "component": "system/partner/index",
                  "meta": {
                    "title": "机构管理",
                    "icon": "form",
                    "noCache": false
                  }
                }
              ]
            }

          ]
        }
        const accessedRoutes = filterAsyncRouter(res.data)
        accessedRoutes.push({path: '*', redirect: '/404', hidden: true})
        commit('SET_ROUTES', accessedRoutes)
        resolve(accessedRoutes)
      })
    }
  }
}

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  return asyncRouterMap.filter(route => {
    if (route.component) {
      // Layout组件特殊处理
      if (route.component === 'Layout') {
        route.component = Layout
      } else {
        route.component = loadView(route.component)
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    return true
  })
}

export const loadView = (view) => { // 路由懒加载
  return (resolve) => require([`@/views/${view}`], resolve)
}

export default permission
